<?php
$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATE_INTERNAL_FORMAT).' 00:00:00';
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
    ->addUrlRewrite()
    ->addStoreFilter()
    ->addAttributeToFilter('special_from_date', array( 'lteq' => $todayDate),'left')
    ->addAttributeToFilter('special_to_date', array( 'gteq' => $todayDate), 'left')
    ->addAttributeToSort('special_from_date', 'asc')
    ->setPageSize(5)
    ->setCurPage(1)
    ;
//var_dump($todayDate,count($collection));
//echo $collection->getSelect();
//echo Mage::app()->getLocale()->date()->addDay(1)->setHour(0)->setMinute(0)->setSecond(0)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT).'<br/>';
//echo Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT).'<br/>';
$timelimied = Mage::app()->getLocale()->date()->addDay(1)->setHour(0)->setMinute(0)->setSecond(0)->getTimestamp() - Mage::app()->getLocale()->date()->getTimestamp();
     //$days = floor($time/86400);
     $hours = floor($timelimied/3600);
     $mins = floor (($timelimied-($hours*3600))/60);
     $secs = floor ($timelimied-($hours*3600)-($mins*60));
     //var_dump($hours,$mins,$secs);
?>

<?php
    $_helper = $this->helper('catalog/output');
    $_collectionSize = $collection->count();
	if($_collectionSize > 0):
?>
<div class="daily fbox clearfix">
<h3>Daily Deals</h3>
    <?php $_columnCount = 5; ?>
    <?php $i=0; foreach ($collection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-tab <?php if( $_collectionSize <= ($i+$_columnCount)): ?>last<?php endif; ?>">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <div class="tab-product-img"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150); ?>" width="150" height="150" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a></div>                
				<div class="saveoff"><?php  echo ceil(100-((100/$_product->getPrice())*$_product->getFinalPrice())).'%'; ?> OFF</div>
				<div class="tab-other-info">
					<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
				</div>				
				<div class="add-to-cart add-cart-info">
					<?php if( $_product->getRatingSummary()): ?>
						<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
					<?php endif; ?> 
					<?php echo $this->getPriceHtml($_product, true) ?>
				   <?php if($_product->isSaleable()): ?>
							   <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
							<?php else: ?>
					 <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
					<?php endif; ?>
					  <?php if ($this->helper('wishlist')->isAllow()) : ?>
									<div><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></div>
					 <?php endif; ?>
				</div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
	<?php if($i==10) {break;} ?>
    <?php endforeach ?>
</div>
<script type="text/javascript">
var startTime = new Date();
var timeLimied = <?php echo intval($timelimied);?>;
function showTime(){
 var nowTime = new Date();
 var compareTime = timeLimied - parseInt( (nowTime-startTime)/1000 );
 if(compareTime < 0){
     $('timeshow').innerHTML = 'please refresh';
     $('category-products-special').hide();
     return false;
 
 }
 var compareHour = parseInt(compareTime / 3600);
 var compareMinute = parseInt((compareTime-(compareHour*3600))/60);
 var compareSecond = parseInt((compareTime-(compareHour*3600)-(compareMinute*60)));
 compareHour = compareHour<10 ? '0'+compareHour : compareHour;
 compareMinute = compareMinute<10 ? '0'+compareMinute : compareMinute;
 compareSecond = compareSecond<10 ? '0'+compareSecond : compareSecond;
 var html = '<span>'+compareHour+'</span>';
     html += '<span>:</span>';
     html += '<span>'+compareMinute+'</span>';
     html += '<span>:</span>';
     html += '<span>'+compareSecond+'</span>';
  $('timeshow').innerHTML = html;
}
showTime();
setInterval(showTime,1000);

</script>
<?php endif;?>

